﻿package com.eve.eplus.main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.Window;

import com.eve.eplus.R;
import com.eve.eplus.common.app.BaseApplication;
import com.eve.eplus.common.app.Constants;
import com.eve.eplus.common.view.ComToastTools;
import com.eve.eplus.main.Fragment.ContentFragment;
import com.eve.eplus.main.Fragment.MenuFragment;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

/**
 * 主界面
 * 
 * @author Norsie
 * @create 12-11-29
 */
public class HomeActivity extends SlidingFragmentActivity
{
    private Context mContext = HomeActivity.this;
    private Fragment mContent;

    // private Context mContext = HomeActivity.this;
    // private long exitTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        if (savedInstanceState != null)
            BaseApplication.SNS_ACCOUNT = (ArrayList) savedInstanceState.getSerializable("SNS_ACCOUNT");

        if (BaseApplication.SNS_ACCOUNT.size() == 0)
        {
            ComToastTools.ShowMsg(mContext, "请先绑定帐号", "s");
            Intent intent = new Intent(mContext, BindingActivity.class);
            intent.putExtra(Constants.ACTIVITY_TARGET,
                    Constants.ACTIVITY_TARGET_HOMEACTIVITY);
            startActivity(intent);
            finish();
        }
        else
        {

            // set the Above View
            // if (savedInstanceState != null)
            // mContent = getSupportFragmentManager().getFragment(
            // savedInstanceState, "mContent");
            if (mContent == null)
                mContent = new ContentFragment(
                                               BaseApplication.SNS_ACCOUNT.get(0)); // 要求必须有帐号
            // 不然会出错

            // set the Above View
            // 设置主ListView
            setContentView(R.layout.activity_home_content_frame); // 放置容器，使用空的FrameLayout以避免产生叠加的现象
            getSupportFragmentManager().beginTransaction().replace(
                    R.id.home_content_fl, mContent).commit();

            // set the Behind View
            // 设置SlidingMenu的ListView
            setBehindContentView(R.layout.activity_home_menu_frame);
            getSupportFragmentManager().beginTransaction().replace(
                    R.id.home_menu_fl, new MenuFragment()).commit();

            // customize the SlidingMenu
            // 设置Menu效果
            getSlidingMenu().setMode(com.slidingmenu.lib.SlidingMenu.LEFT);
            getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
            getSlidingMenu().setShadowDrawable(
                    R.drawable.homeactivity_menu_shadow);
            getSlidingMenu().setBehindOffsetRes(R.dimen.slidingmenu_offset);
            getSlidingMenu().setFadeDegree(0.35f);
            getSlidingMenu().setTouchModeAbove(
                    com.slidingmenu.lib.SlidingMenu.TOUCHMODE_FULLSCREEN);
        }

    }

    protected void onResume()
    {
        if (BaseApplication.SNS_ACCOUNT.size() == 0)
        {
            ComToastTools.ShowMsg(mContext, "请先绑定帐号", "s");
            Intent intent = new Intent(mContext, BindingActivity.class);
            startActivity(intent);
            finish();
        }
        else
        {
            if (mContent == null)
                mContent = new ContentFragment(
                                               BaseApplication.SNS_ACCOUNT.get(0)); // 要求必须有帐号
        }
        super.onResume();
        if (getSlidingMenu().isMenuShowing())
        {
            getSlidingMenu().showContent();
        }
    };

    @Override
    public void onBackPressed()
    {
        if (getSlidingMenu().isMenuShowing())
        {
            getSlidingMenu().showContent();
        }
        else
        {
            // 执行退回到桌面的操作
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 如果是服务里调用，必须加入new task标识
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            // super.onBackPressed();
        }
    }

    @Override
    protected void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            getSlidingMenu().toggle();
        }
        return super.onKeyDown(keyCode, event);
    }
    

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        outState.putSerializable("SNS_ACCOUNT", BaseApplication.SNS_ACCOUNT);
        super.onSaveInstanceState(outState);
    }

    public void switchContent(Fragment fragment)
    {
        mContent = fragment;
        getSupportFragmentManager().beginTransaction().replace(
                R.id.home_content_fl, fragment).commit();
        getSlidingMenu().showContent();
    }

}
